package dht.message;
import java.util.HashMap;
import java.util.Map;

public enum OperationType {

		GreaterThan(1),
		LessThan(2),
		Equal(3),
		Average(4),
		Put(5),
		Unknown(6);		
		
		
		private static final Map<Short, OperationType> map = new HashMap<>();
		static {
			for (OperationType qc : OperationType.values())
				map.put(new Short(qc.value()), qc);
		}

		private final short code;
		private OperationType(int code)
		{
			this((short)code);
		}
		private OperationType(short code)
		{
			this.code = code;
		}

		/**
		 * @return the short value associated with this OperationType
		 */
		 public short value()
		{
			return code;
		}

		/**
		 * A convenience method. It returns OperationType.convert((short)code);
		 */
		 public static OperationType convert(int code)
		 {
			 return convert((short)code);
		 }

		 /**
		  * @param code a short value corresponding to an OperationType
		  * @return the OperationType corresponding to the specified short, or
		  *          OperationType.Unknown if the short does not map to an OperationType
		  */
		 public static OperationType convert(short code)
		 {
			 OperationType qc = map.get(code);
			 if (qc == null) {
				 return OperationType.Unknown;
			 }
			 return qc;
		 }
		 
		public static OperationType getType(String type) {
			switch (type) {
			case "GreaterThan":
				return convert(1);
			case "LessThan":
				return convert(2);
			case "Equal":
				return convert(3);
			case "Average":
				return convert(4);
			default:
				return convert(5);
			}
		 }
}
